package com.tencreat.common.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.lock.spring.boot.autoconfigure.Lock4jProperties;
import com.tencreat.common.utils.spring.SpringUtils;

import java.util.Collections;
import java.util.List;

/**
 * 描述信息Lock4jUtil
 *
 * @author zixin
 * @company 深圳腾创互联科技有限公司
 * @link https://tencreat.com
 * @date 2023/1/3 09:41
 */
public class Lock4jUtil {

    /**
     * 获取锁key
     * 与@Lock4j锁key保持一致
     *
     * @param name 名称
     * @param keys 参数...
     */
    public static String getLockKey(String name, List<String> keys) {
        Lock4jProperties lock4jProperties = SpringUtils.getBean(Lock4jProperties.class);
        String prefix = lock4jProperties.getLockKeyPrefix() + ":";
        prefix += name;
        String suffix = StrUtil.join(".", keys);
        String key = prefix + "#";
        if (null != suffix) {
            key += suffix;
        }
        return key;
    }

    public static String getLockKey(String name) {
        return getLockKey(name, null);
    }

    public static String getLockSingleKey(String name, String key) {
        return getLockKey(name, Collections.singletonList(key));
    }

}
